import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_module/tabbar/shop/shopTagModel.dart';
import 'package:flutter_module/tabbar/shop/shop_row_widget.dart';
import 'package:flutter_module/tool/shopDeviceManager.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:provider/provider.dart';
import 'shopScreenProvider.dart';
import 'shop_screen_tag_provider.dart';

class ShopScreenTag extends StatefulWidget {
  @override
  _ShopScreenTagState createState() => _ShopScreenTagState();
}

class _ShopScreenTagState extends State<ShopScreenTag> with AutomaticKeepAliveClientMixin {

  // 必须重写
  @override
  bool get wantKeepAlive => true;

  EasyRefreshController _controller;
  bool _isTag = true;

  List<dynamic> _originalShopTags;

  @override
  void initState() { 
    super.initState();
    _controller = EasyRefreshController();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);

    return MultiProvider(
      providers: [ChangeNotifierProvider.value(value: ShopScreenTagProvider())],
      child: Column(
        children: <Widget>[
          shopHeaderWidget(context),
          shopListView(context)
        ],
      ),
    ); 
  }

  Widget shopHeaderWidget(BuildContext context){

    return Consumer<ShopScreenTagProvider>(
      builder: (context,provider,child){

        List<ShopTagModel> _headerList = provider.headerList;
        if (_headerList.length == 0) {
          _headerList = provider.defalutShopScreenTagHeaderList();
        }
        return Container(
          height: 70,
          color: Color.fromRGBO(235, 239, 244, 1),
          child: Container(
            color: Colors.white,
            margin: EdgeInsets.fromLTRB(0, 10, 0, 10),
            child: ListView.builder(
              scrollDirection: Axis.horizontal,
              physics:  ClampingScrollPhysics(),
              itemCount: _headerList.length,
              itemBuilder: (context,index){
                return shopHeaderItemWidget(_headerList[index],index);
              },
             ),
          )
        );
      },
    );
  }

  Widget shopListView(BuildContext context){

    return FutureBuilder(
      future: ShopDeviceManager.fetchShopTags(),
      builder: (context,snapshot){
        if (!snapshot.hasData) {
          return Center(
            child: SpinKitCircle(color: Colors.lightBlue,),
          );
        }
        print('object');
        return Consumer2<ShopScreenProvider,ShopScreenTagProvider>(
          builder: (context,provider,tagProvider,child){
            if (_isTag) {
              provider.setShopTags(snapshot.data);
            }
            _isTag = true;
            List<dynamic> _shopTags = provider.shopTags;
            _originalShopTags = _shopTags;
            if (_shopTags.length == 0) {
              return Center(
                child: Text('暂无内容'),
              );
            }
            return Expanded(
              child: EasyRefresh(
                  controller: _controller,
                  enableControlFinishRefresh: true,
                  child: ListView.builder(
                    shrinkWrap: true,
                    itemCount: _shopTags.length,
                    itemBuilder: (context, index) {
                      dynamic model = _shopTags[index];
                      if (model is ShopTagModel) {
                        return shopTagWidget(_shopTags[index],(){
                          _isTag = false;
                          provider.setShopTags(_shopTags[index].tagShops);

                          List<ShopTagModel> _headerList = tagProvider.headerList;
                          _headerList.add(_shopTags[index]);
                          tagProvider.setShopScreenTagHeaderList(_headerList);
                        });
                      }else{
                        return ShopRowWidget(model:model);
                      }
                    },
                ),
                onRefresh: () async {
                  ShopDeviceManager.fetchShopTags().then((val){
                    _controller.finishRefresh();
                  });
                },
              )
            );
          },
        );
      },
    );
  }

  Widget shopHeaderItemWidget(ShopTagModel model,int index){
    return Container(
      color: Colors.white,
      child: Center(
        child: Row(
          children: <Widget>[
            Offstage(
              offstage: index == 0 ? true : false,
              child: Image.asset('images/common_arrow_gray_right.png'),
            ),
            SizedBox(width: 10),
            Consumer2<ShopScreenProvider,ShopScreenTagProvider>(
              builder: (context,provider,tagProvider,child){
                return InkWell(
                  onTap: (){
                    if (index != 0) return;
                    provider.setShopTags(_originalShopTags);
                    tagProvider.reloadShopScreenTagHeaderList(tagProvider.defalutShopScreenTagHeaderList());
                  },
                  child: Text('${model.name}',style: TextStyle(color: Color.fromRGBO(46, 137, 250, 1))),
                );
              },
            ),
            SizedBox(width: 10),
          ],
        ),
      ),
    );
  }

  Widget shopTagWidget(ShopTagModel shopTagModel,Function fun){

    return InkWell(
      onTap: (){
        fun();
      },
      child: Container(
        height: 60,
        color: Colors.white,
        margin: EdgeInsets.only(bottom: 2.5),
        child: Flex(
          direction: Axis.horizontal,
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: <Widget>[
            SizedBox(width: 15),
            Expanded(
              child: Text(
                shopTagModel.name,
                overflow: TextOverflow.ellipsis,
              ),
            ),
            Text('[${shopTagModel.tagShops.length}]',style: TextStyle(color: Color.fromRGBO(102, 102, 102, 1))),
            SizedBox(width: 10),
            Padding(
              padding: EdgeInsets.only(right: 15),
              child: Image.asset('images/common_arrow_gray_right.png'),
            )
          ],
        ),
      ),
    );
  }
}
